package com.jike.noobmoney.mvp.view.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.util.AppInfoUtils;
import com.jike.noobmoney.util.CheckAppInstalledUtil;
import com.jike.noobmoney.util.MobileInfoUtil;
import com.jike.noobmoney.util.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class DianCaiActivity extends BaseActivity {


    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @Override
    protected int layoutResID() {
        return R.layout.activity_diancai_web;
    }

    @Override
    protected void initData() {
        tvTitle.setText("点财");
        // 启用javascript
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        String userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        String url="http://www.dianmoney.com/diancai/gamelistnew?imei="+ MobileInfoUtil.getIMEI(DianCaiActivity.this)+"&appId="+"17885"+"&userId="+userid;
        webView.loadUrl(url);
        webView.addJavascriptInterface(DianCaiActivity.this, "android");//name:android在网页里面可以用window.name.方法名调用java方法

    }
    @JavascriptInterface
    public void openApp(String packageName,String downloadUrl) {
        //根据包名称判断是否安装，已安装直接打开APP，没有安装的调用下载地址先下载
        //实现下载并提示安装功能
        if(CheckAppInstalledUtil.isInstalled(this,packageName)){//判断是否安装
            CheckAppInstalledUtil.openApp(this, packageName); //打开应用
        }else{//没有安装
            //TODO 去下载
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        }

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
